<template>
  <div>{{ displayName }}</div>
</template>

<script>
import {getRespData, isNotNull, isNull, Timeout} from "@/util/common-util";
import {getChildList} from "@/api/business/dict";
import {dictLabelMap} from "@/components/business/global-filter";

export default {
  name: "dict-value-display",
  props: ['value', 'dictCode'],
  data() {
    return {
      displayName: '-'
    }
  },
  async created() {
    await this.initDictCode()
  },
  methods: {
    async initDictCode() {
      if (isNull(dictLabelMap[this.dictCode])) {
        dictLabelMap[this.dictCode] = {}
        await getChildList(this.dictCode).then(res => {
          for (const item of getRespData(res)) {
            dictLabelMap[this.dictCode][item.dictKey.toString()] = item.dictValue
          }
        })
      }
      const re = dictLabelMap[this.dictCode][this.value.toString()]
      if (isNotNull(re)) {
        this.parseValue()
        return
      }
      new Timeout(1000).handle(() => {
        this.parseValue()
      })
    },
    parseValue() {
      const re = dictLabelMap[this.dictCode][this.value.toString()]
      this.displayName = isNull(re) ? this.value : re;
    }
  },
}

</script>

<style lang="scss" scoped>

</style>
